﻿using System.Collections.Generic;
using System.Globalization;
using Infrastructure.IP.Interfaces;
using UsersProvider.Repositories.RelationalRepositories;

namespace Infrastructure.IP
{
   public class IpToCultureInfoConverter : IIpToCultureInfoConverter
   {
      private readonly Dictionary<string, string> _countryCodeMappings = new Dictionary<string, string>
                                                                               {
                                                                                   {"ru", "ru"}, //Russia
                                                                                   {"by", "ru"}, //Belarussia
                                                                                   {"ua", "ru"}, //Ukraine
                                                                                   {"ge", "ru"}, //Georgia
                                                                                   {"tm", "ru"}, //Turkmenistan
                                                                                   {"uz", "ru"}, //Uzbekistan
                                                                                   {"tj", "ru"}, //Tajikistan
                                                                                   {"az", "ru"}, //Azerbaijan
                                                                                   {"am", "ru"}, //Armenia
                                                                                   {"kz", "ru"}, //Kazakhstan
                                                                                   {"kg", "ru"}, //Kyrgyzstan
                                                                                   {"md", "ru"}, //Moldova
                                                                                   {"lt", "ru"}, //Lithuania
                                                                                   {"lv", "ru"}, //Latvia
                                                                                   {"ee", "ru"}  //Estonia
                                                                               };

      private readonly CultureInfo _defaultCulture = CultureInfo.GetCultureInfo("en");

      private readonly IIpToNumberConverter _ipToNumberConverter;
      private readonly ICountryCodesRepository _countryCodesRepository;

      public IpToCultureInfoConverter(IIpToNumberConverter ipToNumberConverter,
                                      ICountryCodesRepository countryCodesRepository)
      {
         _ipToNumberConverter = ipToNumberConverter;
         _countryCodesRepository = countryCodesRepository;
      }

      public CultureInfo GetCultureInfo(string ipAddress)
      {
         long ipNumber = _ipToNumberConverter.IpToNumber(ipAddress);
         string countryCode = _countryCodesRepository.GetCountryCode(ipNumber);

         if (countryCode == null)
            return _defaultCulture;

            return _countryCodeMappings.ContainsKey(countryCode)
                       ? CultureInfo.GetCultureInfo(_countryCodeMappings[countryCode])
                       : _defaultCulture;
      }
   }
}